package ymy;

import ymy.common.*;
import java.util.*;


public class RandomSimulator extends BaseSimulator {

    public RandomSimulator(Environment env) {
        super(env);
    }

    @Override
    public void vehicleMoveAction(Vehicle v, double duration) {
        int direct = v.getDirection();
        Coordinate coor = v.getPosition();
        double x = coor.getX();
        double y = coor.getY();
        double len = v.getSpeed() * duration;

        while (true) {
            RoutePoint rp = this.environment.moveByRoutePoint(coor, len, direct);

            if (rp == null) {
                if (direct == GeneralConfig.WEST)
                    x = x - len;
                if (direct == GeneralConfig.EAST)
                    x = x + len;
                if (direct == GeneralConfig.SOUTH)
                    y = y - len;
                if (direct == GeneralConfig.NORTH)
                    y = y + len;

                coor.setX(x);
                coor.setY(y);
                break;
            } else {
                double rpx = rp.getPosition().getX();
                double rpy = rp.getPosition().getY();
                double tmp = 0.0;

                if (rpx == x)
                    tmp = Math.abs(rpy - y);
                else if (rpy == y)
                    tmp = Math.abs(rpx - x);

                x = rpx;
                y = rpy;
                coor.setX(x);
                coor.setY(y);

                do {
                    direct = rp.nextRandomDirection(direct);
                } while (direct == GeneralConfig.UNKNOWN);

                len = len - tmp;
            }
        }

        v.setPosition(coor);
        v.setDirection(direct);

        if (GeneralConfig.DEBUG_UPDATE) {
            System.out.println("Vehicle ID =  " + v.getId());
            System.out.println("Vehicle Coordinate = " + coor);
        }
    }

    @Override
    public void initParkVehicles() {
        if (GeneralConfig.DEBUG_INFO)
            System.out.println("[INFO]随机初始化停放车辆信息...");
        for (int i = 0; i < RandomConfig.PARK_VEHICLES; i++) {
            // 获取停放车辆的停车点信息
            Environment.ParkLocation pl = this.environment.getRandomParkLocation();

            Vehicle pv = new Vehicle();
            pv.setId(this.vid++);
            pv.setParked(true);
            pv.setRunning(true);
            pv.setPosition(Utility.copyOfCoordinate(pl.getCoordinate()));
            pv.setDirection(GeneralConfig.UNKNOWN);
            pv.setArea(pl.getArea());
            pv.setDelay(GeneralConfig.VEHICLE_PARK_PTIME);
            pv.setGain(1.0);
            pv.setSpeed(0);
            pv.setMoment(0);
            pv.setDistance(0);

            if (GeneralConfig.DEBUG_INIT) {
                System.out.println("[INFO]第" + (i + 1) + "辆停驻车辆信息：");
                System.out.println(pv);
            }

            this.parkVehicleMap.put(pv.getId(), pv);
        }
        if (GeneralConfig.DEBUG_INFO)
            System.out.println("[INFO]成功添加" + this.parkVehicleMap.size() + "辆停驻车辆！！");
    }

    @Override
    public void initMoveVehiclesOV() {

    }

    @Override
    public void initParkVehiclesOV() {

    }

    @Override
    public void initMoveVehicles() {
        if (GeneralConfig.DEBUG_INFO)
            System.out.println("[INFO]随机初始化移动车辆信息...");
        Random random = new Random(System.currentTimeMillis());
        for (int i = 0; i < RandomConfig.MOVE_VEHICLES; i++) {
            // 获取移动车辆发车点信息
            Environment.MoveLocation wl = this.environment.getRandomMoveLocation();

            // 随机得到移动车辆的速度
            int speed_ten_times_range = (int)((GeneralConfig.VEHICLE_UPPER_SPEED - GeneralConfig.VEHICLE_LOWER_SPEED) * 10.0);
            double speed = random.nextInt(speed_ten_times_range) / 10.0 + GeneralConfig.VEHICLE_LOWER_SPEED;

            // 随机得到移动车辆的发车时刻
            // 随机得到移动车辆的发车时刻
            double time = 0;
            //time = (double)random.nextInt((int) SpecificConfig.SIMU_DURATION / 10);

            Vehicle wv = new Vehicle();
            wv.setId(this.vid++);
            wv.setParked(false);
            wv.setRunning(false);
            wv.setPosition(Utility.copyOfCoordinate(wl.getCoordinate()));
            wv.setDirection(wl.getDirection());
            wv.setArea(null);
            wv.setSpeed(speed);
            wv.setDelay(GeneralConfig.VEHICLE_MOVE_PTIME);
            wv.setGain(GeneralConfig.VEHICLE_GAIN);
            wv.setMoment(time);
            wv.setDistance(0);

            if (GeneralConfig.DEBUG_INIT) {
                System.out.println("[INFO]第" + (i + 1) + "辆移动车辆信息：");
                System.out.println(wv);
            }

            this.moveVehicleMap.put(wv.getId(), wv);
        }
        if (GeneralConfig.DEBUG_INFO)
            System.out.println("[INFO]成功添加" + this.moveVehicleMap.size() + "辆移动车辆！！");
    }

    public StageResult randomSimulationProcess(boolean ov) {
        int helloCount = (int)(RandomConfig.SAMPLE_PERIOD / RandomConfig.HELLO_PERIOD);
        int sampleCount = (int)(RandomConfig.PROCEED_PERIOD / RandomConfig.SAMPLE_PERIOD);
        int processCount = (int)(RandomConfig.SIMU_DURATION / RandomConfig.PROCEED_PERIOD);

        return this.simulationProcess(RandomConfig.SAMPLE_PERIOD, helloCount, sampleCount, processCount, ov);
    }
}
